#!/bin/bash
##############################################################
source ./pkgutils.sh || exit 1
PKG_NAME=$(pkgname); if (( $? != 0)); then exit 2; fi
PKG_VERSION=$(pkgversion); if (( $? != 0)); then exit 3; fi
if [[ $(whoami) != "root" ]]; then
	echo "Please log in as user root...";
	exit 4;
fi
mkdir -pv ./tmp && cd ./tmp || exit 5;
echo "setup of $PKG_NAME $PKG_VERSION";
##############################################################
# do setup tasks (location: /usr/pkg/$PKG_NAME/$PKG_VERSION/tmp)

# Create (or update) and populate the /etc/xml/docbook catalog file by running the
#          following commands as the root user:
if [ ! -e /etc/xml/docbook ]; then
    xmlcatalog --noout --create /etc/xml/docbook && \
    echo "/etc/xml/docbook" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files || exit 10;
fi && \
xmlcatalog --noout --add "public" \
    "-//OASIS//DTD DocBook XML V$PKG_VERSION//EN" \
    "http://www.oasis-open.org/docbook/xml/$PKG_VERSION/docbookx.dtd" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "public" \
    "-//OASIS//DTD DocBook XML CALS Table Model V$PKG_VERSION//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION/calstblx.dtd" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "public" \
    "-//OASIS//DTD XML Exchange Table Model 19990315//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION/soextblx.dtd" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "public" \
    "-//OASIS//ELEMENTS DocBook XML Information Pool V$PKG_VERSION//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION/dbpoolx.mod" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "public" \
    "-//OASIS//ELEMENTS DocBook XML Document Hierarchy V$PKG_VERSION//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION/dbhierx.mod" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "public" \
    "-//OASIS//ELEMENTS DocBook XML HTML Tables V$PKG_VERSION//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION/htmltblx.mod" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "public" \
    "-//OASIS//ENTITIES DocBook XML Notations V$PKG_VERSION//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION/dbnotnx.mod" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "public" \
    "-//OASIS//ENTITIES DocBook XML Character Entities V$PKG_VERSION//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION/dbcentx.mod" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "public" \
    "-//OASIS//ENTITIES DocBook XML Additional General Entities V$PKG_VERSION//EN" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION/dbgenent.mod" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "rewriteSystem" \
    "http://www.oasis-open.org/docbook/xml/$PKG_VERSION" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION" \
    /etc/xml/docbook && \
xmlcatalog --noout --add "rewriteURI" \
    "http://www.oasis-open.org/docbook/xml/$PKG_VERSION" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION" \
    /etc/xml/docbook || exit 11;

# Create (or update) and populate the /etc/xml/catalog catalog file by running the
#          following commands as the root user:
if [ ! -e /etc/xml/catalog ]; then
    xmlcatalog --noout --create /etc/xml/catalog && \
    echo "/etc/xml/catalog" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files || exit 11;
fi && \
xmlcatalog --noout --add "delegatePublic" \
    "-//OASIS//ENTITIES DocBook XML" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog && \
xmlcatalog --noout --add "delegatePublic" \
    "-//OASIS//DTD DocBook XML" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog && \
xmlcatalog --noout --add "delegateSystem" \
    "http://www.oasis-open.org/docbook/" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog && \
xmlcatalog --noout --add "delegateURI" \
    "http://www.oasis-open.org/docbook/" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog || exit 12;

for DTDVERSION in 4.1.2 4.2 4.3 4.4
do
  if [[ $(pkginstalled $PKG_NAME $DTDVERSION) != "" ]]; then
    continue;
  fi
  xmlcatalog --noout --add "public" \
    "-//OASIS//DTD DocBook XML V$DTDVERSION//EN" \
    "http://www.oasis-open.org/docbook/xml/$DTDVERSION/docbookx.dtd" \
    /etc/xml/docbook && \
  xmlcatalog --noout --add "rewriteSystem" \
    "http://www.oasis-open.org/docbook/xml/$DTDVERSION" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION" \
    /etc/xml/docbook && \
  xmlcatalog --noout --add "rewriteURI" \
    "http://www.oasis-open.org/docbook/xml/$DTDVERSION" \
    "file:///usr/share/xml/docbook/xml-dtd-$PKG_VERSION" \
    /etc/xml/docbook && \
  xmlcatalog --noout --add "delegateSystem" \
    "http://www.oasis-open.org/docbook/xml/$DTDVERSION/" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog && \
  xmlcatalog --noout --add "delegateURI" \
    "http://www.oasis-open.org/docbook/xml/$DTDVERSION/" \
    "file:///etc/xml/docbook" \
    /etc/xml/catalog || exit 13;
done


# hint: write all file modifications/copy/mv/ln into: /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files
##############################################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package setup with code $ERR";
    exit 6;
fi
